import { useEffect, useRef } from 'react';

/**
 *  防抖
 * @param fn 回调函数
 * @param ms 防抖间隔
 * @param deps 依赖数组
 * @returns
 */
const useDebounce = (fn: any, ms = 30, deps = []) => {
    let timeout = useRef();
    useEffect(() => {
        if (timeout.current) {
            clearTimeout(timeout.current);
        }
        // @ts-ignore
        timeout.current = setTimeout(() => {
            fn();
        }, ms);
    }, deps);
    const cancel = () => {
        clearTimeout(timeout.current);
        // @ts-ignore
        timeout = null;
    };
    return [cancel];
};
export default useDebounce;
